
d = {}
for i in xrange(1, 1000000):
    p = i ** 3
    ps = list(str(p))
    ps.sort()
    ps = ''.join(ps)
    if ps not in d:
        d[ps] = []
    d[ps].append(p)

ans = 2 ** 62

for ps in d:
    if len(d[ps]) == 5:
        if ans > d[ps][0]:
            ans = d[ps][0]

print ans

